from flask import Flask,redirect,render_template,url_for
import pandas as pd
import re
import collections
import json
import jieba.analyse
from collections import Counter
import Amysql

app = Flask(__name__)


@app.route('/')
def hello_world():
    return render_template('bigdata.html')

@app.route('/next')
def next():
    return render_template('index.html')


@app.route('/data',methods=['get','post'])
def name_data():
    data = pd.read_csv('boss1.csv')

    data['post'] = data['title']
    data['post'][data['post'].str.contains(r'开发|研发')] = '大数据开发工程师'
    data['post'][data['post'].str.contains(r'架构')] = '大数据架构工程师'
    data['post'][data['post'].str.contains(r'挖掘')] = '数据挖掘工程师'
    data['post'][data['post'].str.contains(r'运维|维护')] = '数据运维工程师'
    data['post'][data['post'].str.contains(r'分析')] = '数据分析师'
    data['post'][data['post'].str.contains(r'算法')] = '算法工程师'
    data['post'][data['post'].str.contains(r'可视化')] = '大数据可视化工程师'
    data[~ data['post'].str.contains('开发|架构|挖掘|运维|分析|算法|可视化')] = '其他'

    a = ""
    b = ""
    c = ""
    d = ""
    e = ""
    f = ""
    g = ""
    h = ""
    num = 0
    for line in data['post']:
        if line == '大数据开发工程师':
            a += str(data['need'][num])
        elif line == '大数据架构工程师':
            b += str(data['need'][num])
        elif line == '数据挖掘工程师':
            c += str(data['need'][num])
        elif line == '数据运维工程师':
            d += str(data['need'][num])
        elif line == '数据分析师':
            e += str(data['need'][num])
        elif line == '算法工程师':
            f += str(data['need'][num])
        elif line == '大数据可视化工程师':
            g += str(data['need'][num])
        elif line == '其他':
            h += str(data['need'][num])
        num += 1

    def getre(data):
        name = []
        value = []
        resa = []
        res = re.findall('[a-zA-Z]+', data)
        for i in res:
            i = i.capitalize()
            resa.append(i)
        Word_count = collections.Counter(resa)
        new = Word_count.most_common(1)
        # print(Word_count)
        # for word,key in Word_count:
        #     name.append(word)
        #     value.append(key)
        return new

    name = ['大数据开发工程师', '大数据架构工程师', '数据挖掘工程师', '数据运维工程师', '数据分析师', '算法工程师', '大数据可视化工程师']
    jsonData = {}
    name_num = []
    wor = []
    value = []
    for i in [a, b, c, d, e, f, g]:
        i = str(i)
        aa = getre(i)
        words = []
        keys = []
        for word, key in aa:
            words.append(word)
            keys.append(key)
        name_num.append(sum(keys))
        wor += words
        value += keys

    jsonData['name'] = name
    jsonData['name_num'] = name_num
    jsonData['wor'] = wor
    jsonData['value'] = value

    j = json.dumps(jsonData)
    return j

@app.route('/dat',methods=['get','post'])
def name_dat():
    data = pd.read_csv('boss1.csv')

    data['post'] = data['title']
    data['post'][data['post'].str.contains(r'开发|研发')] = '大数据开发工程师'
    data['post'][data['post'].str.contains(r'架构')] = '大数据架构工程师'
    data['post'][data['post'].str.contains(r'挖掘')] = '数据挖掘工程师'
    data['post'][data['post'].str.contains(r'运维|维护')] = '数据运维工程师'
    data['post'][data['post'].str.contains(r'分析')] = '数据分析师'
    data['post'][data['post'].str.contains(r'算法')] = '算法工程师'
    data['post'][data['post'].str.contains(r'可视化')] = '大数据可视化工程师'
    data[~ data['post'].str.contains('开发|架构|挖掘|运维|分析|算法|可视化')] = '其他'

    a = ""
    b = ""
    c = ""
    d = ""
    e = ""
    f = ""
    g = ""
    h = ""
    num = 0
    for line in data['post']:
        if line == '大数据开发工程师':
            a += str(data['need'][num])
        elif line == '大数据架构工程师':
            b += str(data['need'][num])
        elif line == '数据挖掘工程师':
            c += str(data['need'][num])
        elif line == '数据运维工程师':
            d += str(data['need'][num])
        elif line == '数据分析师':
            e += str(data['need'][num])
        elif line == '算法工程师':
            f += str(data['need'][num])
        elif line == '大数据可视化工程师':
            g += str(data['need'][num])
        elif line == '其他':
            h += str(data['need'][num])
        num += 1

    def getre(data):
        name = []
        value = []
        resa = []
        res = re.findall('[a-zA-Z]+', data)
        for i in res:
            i = i.capitalize()
            resa.append(i)
        Word_count = collections.Counter(resa)
        new = Word_count.most_common(5)
        # print(Word_count)
        # for word,key in Word_count:
        #     name.append(word)
        #     value.append(key)
        return new

    name = ['大数据开发工程师', '大数据架构工程师', '数据挖掘工程师', '数据运维工程师', '数据分析师', '算法工程师', '大数据可视化工程师']

    def getaa(aaa):
        i = str(aaa)
        aa = getre(i)
        words = []
        keys = []
        for word, key in aa:
            words.append(word)
            keys.append(key)
        return words, keys

    worda, keya = getaa(a)
    wordb, keyb = getaa(b)
    wordc, keyc = getaa(c)
    wordd, keyd = getaa(d)
    worde, keye = getaa(e)
    wordf, keyf = getaa(f)
    wordg, keyg = getaa(g)

    jsonData = {}
    name_num = []

    name_num = [sum(keya), sum(keyb), sum(keyc), sum(keyd), sum(keye), sum(keyf), sum(keyg)]
    jsonData['name'] = name
    jsonData['name_num'] = name_num
    jsonData['keya'] = keya
    jsonData['keyb'] = keyb
    jsonData['keyc'] = keyc
    jsonData['keyd'] = keyd
    jsonData['keye'] = keye
    jsonData['keyf'] = keyf
    jsonData['keyg'] = keyg
    jsonData['worda'] = worda
    jsonData['wordb'] = wordb
    jsonData['wordc'] = wordc
    jsonData['wordd'] = wordd
    jsonData['worde'] = worde
    jsonData['wordf'] = wordf
    jsonData['wordg'] = wordg
    j = json.dumps(jsonData)
    return j

@app.route('/da',methods=['get','post'])
def name_da():
    data_need = pd.read_csv('boss1.csv')
    jieba.load_userdict(r'C:\Users\apple\Desktop\1123.txt')
    jieba.analyse.set_idf_path(r'C:\Users\apple\Desktop\userdict.txt.big')
    a = []
    b = []
    c = []
    num = 0
    for line in data_need['edu']:
        if line == '大专':
            texts1 = jieba.analyse.extract_tags(str(data_need['need'][num]), topK=5, withWeight=False, allowPOS=())
            a += texts1
        if line == '本科':
            texts1 = jieba.analyse.extract_tags(str(data_need['need'][num]), topK=5, withWeight=False, allowPOS=())
            b += texts1
        if line == '硕士':
            texts1 = jieba.analyse.extract_tags(str(data_need['need'][num]), topK=5, withWeight=False, allowPOS=())
            c += texts1
        num += 1
    a = Counter(a).most_common(30)
    b = Counter(b).most_common(30)
    c = Counter(c).most_common(30)

    x_w = []
    x_words = []
    for i in data_need['need']:
        text = jieba.analyse.extract_tags(str(i), topK=5, withWeight=False, allowPOS=())
        for ac in text:
            x_w.append(ac)

    x_word = Counter(x_w).most_common(30)
    for word, key in x_word:
        x_words.append(word)

    keya = []

    for aa in x_words:
        n_n = 0
        for word, key in a:
            n_n += 1
            if word == aa:
                keya.append(key)
                break
            else:
                if n_n == len(x_words):
                    keya.append(0)

    keyb = []

    for aa in x_words:
        n_n = 0
        for word, key in b:
            n_n += 1
            if word == aa:
                keyb.append(key)
                break
            else:
                if n_n == len(x_words):
                    keyb.append(0)

    keyc = []

    for aa in x_words:
        n_n = 0
        for word, key in c:
            n_n += 1
            if word == aa:
                keyc.append(key)
                break
            else:
                if n_n == len(x_words):
                    keyc.append(0)

    jsonData = {}
    jsonData['x_words'] = x_words
    jsonData['keya'] = keya
    jsonData['keyb'] = keyb
    jsonData['keyc'] = keyc

    j = json.dumps(jsonData)
    return j

@app.route('/txt_data',methods=['get','post'])
def xtx():
    data,pd_data = Amysql.Afetchall('localhost', 'root', '123456', 'apple', 'select * from txt_data;')
    word=[]
    value=[]
    for i in data:
        word.append(i[0])
        value.append(i[1])

    jsonData = {}
    jsonData['word'] = word
    jsonData['value'] =value

    j = json.dumps(jsonData)
    return j

@app.route('/ac')
def aaa():
    return render_template('word3.html')

if __name__ == '__main__':
    app.run(debug=True)